package catchData.core;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

import util.CatchDataUtil;
import util.Conts;

/**
 * @author suwy
 * @date 2014-2-7
 * @todo TODO 根据新的ids更新参考数据库数据
 */
public class CountryMedIds extends Thread {
	
	private static final Logger LOG = Logger.getLogger(CountryMedIds.class);
	
	public CountryMedIds() {
		super();
	}
	
	private int startPage;
	private int endPage;
	private String bcId;
	private int tableId;
	private String fileName;
	
	public CountryMedIds(int startPage, int endPage, String bcId, int tableId, String fileName) {
		this.startPage = startPage;
		this.endPage = endPage;
		this.bcId = bcId;
		this.tableId = tableId;
		this.fileName = fileName;
	}
	
	public void run() {
		HttpClient client = CatchDataUtil.initHttpClient();
		PostMethod postMethod = null;
		String regex = "&Id=([^']*)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;
		HashSet<String> webIds = new HashSet<String>();
		String webUrl = Conts.WEBURL
				+ tableId
				+ "&keyword=&bcId="
				+ bcId
				+ "&tableName=TABLE"
				+ tableId
				+ "&viewtitleName=COLUMN167&viewsubTitleName=COLUMN1662,COLUMN1702,COLUMN821&curstart=";
		try {
			for (int i = startPage; i <= endPage; i++) {
				postMethod = new PostMethod(webUrl + i);
				CatchDataUtil.setHeaders(postMethod, webUrl + i,
						CatchDataUtil.getWebSite(webUrl));
				client.executeMethod(postMethod);
				matcher = pattern.matcher(postMethod.getResponseBodyAsString());
				while (matcher.find()) {
					webIds.add(matcher.group().replaceAll("&Id=", "") + "," + i + ",\n");
				}
				LOG.info("读取药监局-" + fileName + "页数：" + i);
				postMethod.releaseConnection();// 释放链接
				if (webIds.size() > 100) {
					writeWebSet(webIds);
					webIds.clear();//清空集合
				} else if(i == endPage) {
					writeWebSet(webIds);
					webIds.clear();//清空集合
				}
			}
		} catch (Exception e) {
			LOG.info(e);
		} finally {
			postMethod.releaseConnection();// 释放链接
		}
	}

	private void writeWebSet(HashSet<String> webIds) {
		File txtFile = new File(Conts.HTM_ROOT_PATH + fileName +".csv");
		FileWriter fw = null;
		try {
			fw = new FileWriter(txtFile,true);
			for (Iterator<String> iterator = webIds.iterator(); iterator.hasNext();) {
				fw.append(iterator.next());
			}
			fw.flush();
        } catch (IOException e) {
			LOG.info(e);
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				LOG.info(e);
			}
		}
	}
	
	/**
	 * @return the startPage
	 */
	public int getStartPage() {
		return startPage;
	}

	/**
	 * @param startPage the startPage to set
	 */
	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	/**
	 * @return the endPage
	 */
	public int getEndPage() {
		return endPage;
	}

	/**
	 * @param endPage the endPage to set
	 */
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

	/**
	 * @return the bcId
	 */
	public String getBcId() {
		return bcId;
	}

	/**
	 * @param bcId the bcId to set
	 */
	public void setBcId(String bcId) {
		this.bcId = bcId;
	}

	/**
	 * @return the tableId
	 */
	public int getTableId() {
		return tableId;
	}

	/**
	 * @param tableId the tableId to set
	 */
	public void setTableId(int tableId) {
		this.tableId = tableId;
	}

	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @param fileName the fileName to set
	 */
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
}